<template>
  <div>
    <el-form ref="form" label-width="80px" :model="skuInfo">
      <!-- SPU名称 -->
      <el-form-item label="SPU名称">{{ spu.spuName }}</el-form-item>
      <!-- SKU名称 -->
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <!-- 价格 -->
      <el-form-item label="价格(元)">
        <el-input type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <!-- 重量 -->
      <el-form-item label="重量(KG)">
        <el-input placeholder="重量(KG)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <!-- 规格描述 -->
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="规格描述"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <!-- 平台属性 -->
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfo"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                v-for="item in attr.attrValueList"
                :key="item.id"
                :label="item.valueName"
                :value="`${attr.id}:${item.id}`"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <!-- 销售属性 -->
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="sale.saleAttrName"
            v-for="sale in saleAttrList"
            :key="sale.id"
          >
            <el-select placeholder="请选择" v-model="sale.saleIdAndValueId">
              <el-option
                :label="saleValue.saleAttrValueName"
                :value="`${sale.id}:${saleValue.id}`"
                v-for="saleValue in sale.spuSaleAttrValueList"
                :key="saleValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <!-- 图片列表 -->
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="SkuImgList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            align="center"
            label="label"
            width="80px"
          >
          </el-table-column>
          <el-table-column label="图片" width="width">
            <template slot-scope="{ row }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button size="mini" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <!-- 保存与取消按钮 -->
      <el-form-item>
        <el-button type="primary" @click="addASku">保存</el-button>
        <el-button @click="clearAndChangeScene">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      //图片列表
      SkuImgList: [],
      //销售属性
      saleAttrList: [],
      //平台属性
      attrInfo: [],
      spu: {},
      //收集
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [
          {
            // imgName: "string",
            // imgUrl: "string",
            // isDefault: "string",
            // spuImgId: 0,
          },
        ],
        //收集平台属性
        skuAttrValueList: [
          {
            attrId: 0,
            valueId: 0,
          },
        ],
        //收集销售属性的字段
        skuSaleAttrValueList: [
          {
            saleAttrId: 0,
            saleAttrValueId: 0,
          },
        ],
      },
    };
  },
  methods: {
    //点击父组件添加sku按钮触发  获取数据
    async getAddSkuList(row, category1Id, category2Id) {
      this.spu = row;
      const { category3Id, id } = row;
      this.skuInfo.category3Id = category3Id;
      //获取图片列表数据
      let imgResult = await this.$API.spu.reqSpuImageList(id);
      if (imgResult.code === 200) {
        let list = imgResult.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.SkuImgList = list;
      }
      //获取销售属性的数据
      let saleResult = await this.$API.spu.reqSpuSaleAttrList(id);
      if (saleResult.code === 200) {
        this.saleAttrList = saleResult.data;
      }
      //获取平台属性的数据
      let attrResult = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        category3Id
      );
      if (attrResult.code === 200) {
        this.attrInfo = attrResult.data;
      }
    },
    //设置谁是   “默认”   排他思想
    changeDefault(row) {
      const { SkuImgList } = this;
      SkuImgList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    //图片列表复选框发生改变时触发
    handleSelectionChange(selection) {
      this.skuInfo.skuImageList = selection;
    },
    //点击保存按钮触发
    async addASku() {
      const { attrInfo, saleAttrList, skuInfo, spu } = this;
      skuInfo.tmId = spu.tmId;
      skuInfo.spuId = spu.id;
      //整理平台属性参数
      skuInfo.skuAttrValueList = attrInfo.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          let [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      //整理销售属性参数
      skuInfo.skuSaleAttrValueList = saleAttrList.reduce((prev, item) => {
        if (item.saleIdAndValueId) {
          let [saleAttrId, saleAttrValueId] = item.saleIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      //整理图片
      skuInfo.skuImageList = skuInfo.skuImageList.reduce((prev, item) => {
        if (item.spuId) {
          delete item.spuId;
          item.spuImgId = item.id;
          delete item.id;
          prev.push(item);
        }
        return prev;
      }, []);
      //发请求
      let result = await this.$API.spu.reqAddSku(skuInfo);
      if (result.code === 200) {
        this.$emit("changeScenes", 0);
        this.$message({ type: "success", message: "添加sku成功" });
        Object.assign(this._data, this.$options.data());
      }
    },
    //点击取消按钮
    clearAndChangeScene() {
      this.$emit("changeScenes", 0);
      Object.assign(this._data, this.$options.data());
    },
  },
};
</script>

<style>
</style>